<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="仓位名称" prop="equipmentName">
        <el-input v-model="queryParams.storeLocationName" placeholder="请输入仓位名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓位编码" prop="equipmentCode">
        <el-input v-model="queryParams.storeLocationCode" placeholder="请输入仓位编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓位状态" prop="storeLocationStatus">
        <el-select v-model="queryParams.storeLocationStatus" placeholder="请选择仓位状态" :onSelectionChange="handleQuery"
          clearable>
          <el-option v-for="dict in dict.type.platform_location_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入监控设备名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备编码" prop="equipmentCode">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入监控设备编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="自动巡检" prop="useInspection">
        <el-select v-model="queryParams.useInspection" placeholder="启用自动巡检" :onSelectionChange="handleQuery" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-select v-model="queryParams.useWarning" placeholder="启用设备" :onSelectionChange="handleQuery" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="开启异常接收" prop="status">
        <el-select v-model="queryParams.useWarning" placeholder="开启异常接收" :onSelectionChange="handleQuery" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="开启异常推送" prop="useWarningPush">
        <el-select v-model="queryParams.useWarningPush" placeholder="开启数字孪生异常推送" :onSelectionChange="handleQuery"
          clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['platform:Location:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['platform:Location:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['platform:Location:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['platform:Location:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LocationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统编号" align="center" prop="storeLocationId" />
      <el-table-column label="仓位名称" width="150" align="center" prop="storeLocationName" :show-overflow-tooltip="true" />
      <el-table-column label="仓位编码" width="80" align="center" prop="storeLocationCode" />
      <el-table-column label="设备名称" width="80" align="center" prop="equipmentName" :show-overflow-tooltip="true" />
      <el-table-column label="设备编码" width="80" align="center" prop="equipmentCode" />
      <el-table-column label="仓库名称" width="80" align="center" prop="storeName" />
      <el-table-column label="初始水平角" width="100" align="center" prop="pan" />
      <el-table-column label="初始垂直角" width="100" align="center" prop="tilt" />
      <el-table-column label="缩放倍数" width="80" align="center" prop="zoom" />
      <el-table-column label="仓位状态" width="80" align="center" prop="storeLocationStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.platform_location_status" :value="scope.row.storeLocationStatus" />
        </template>
      </el-table-column>
      <el-table-column label="货物数量" width="80" align="center" prop="quantity" />
      <el-table-column label="是否启用" align="center" key="useStatus" prop="useStatus" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useStatus" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="自动巡检" width="80" align="center" prop="useInspection" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useInspection" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="异常接收" width="80" align="center" prop="useWarningPush" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useWarning" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="异常推送" width="80" align="center" prop="useWarningPush" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useWarningPush" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="" width="80"  fixed="right">
        <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['platform:Location:edit']">预览</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:Location:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['platform:Location:remove']">删除</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['platform:Location:edit']">预览</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="监控设备：" prop="equipmentId">
          <el-select v-model="form.equipmentId" value="form.equipmentName" filterable remote reserve-keyword
            :remote-method="loadEquipmentList" placeholder="请输入监控设备名称或编码进行搜索" :loading="initData.loading"
            style="width: 500px">
            <el-option v-for="item in initData.equipmentList" :key="item.equipmentId" :label="item.equipmentName"
              :value="item.equipmentId">
              <span style="float: left">{{ item.equipmentName }}</span>
              <span style="
                float: right;
                color: var(--el-text-color-secondary);
                font-size: 13px;
              ">
                {{ item.equipmentCode }}
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓位名称：" prop="equipmentId">
              <el-input v-model="form.storeLocationName" placeholder="请输入仓库名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓位编码：" prop="equipmentId">
              <el-input v-model="form.storeLocationCode" placeholder="请输入仓位编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="初始水平角：" prop="pan">
              <el-input v-model="form.pan" placeholder="请输入初始水平角" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="初始垂直角：" prop="tilt">
              <el-input v-model="form.tilt" placeholder="请输入初始垂直角" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="缩放倍数：" prop="zoom">
              <el-input v-model="form.zoom" placeholder="请输入缩放倍数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货物数量：" prop="quantity">
              <el-input-number v-model="form.quantity" placeholder="请输入货物数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预制点ID" prop="presetId">
              <el-input v-model="form.presetId" placeholder="请输预制点ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓位状态：" prop="storeLocationStatus">
              <el-select v-model="form.storeLocationStatus" placeholder="请选择仓位状态">
                <el-option v-for="item in dict.type.platform_location_status" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开启异常接收：" prop="useWarning">
              <el-switch v-model="form.useWarning" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开启异常推送：" prop="useWarningPush">
              <el-switch v-model="form.useWarningPush" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开启自动巡检：" prop="useInspection">
              <el-switch v-model="form.useInspection" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态：" prop="useStatus">
              <el-switch v-model="form.useStatus" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注：" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--仓位预览-->
    <el-dialog title="仓位预览" :visible.sync="veiwOpen" :close="viewClose" v-loading="this.viewData.loading" width="660px"
      v-if="this.veiwOpen" append-to-body>
      <div>
        <hk-view :ip="this.viewData.ip" :pwd="this.viewData.pwd" :port="this.viewData.port"
          :userName="this.viewData.userName" :pan="this.viewData.pan" :tilt="this.viewData.tilt"
          :iPresetID="this.viewData.iPresetID" :locationId="this.viewData.locationId" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import HkView from './HkView.vue'
import { listLocation, getLocation, delLocation, addLocation, updateLocation } from "@/api/platform/location"
import { listEquipment } from "@/api/platform/equipment"

export default {
  name: "Location",
  components: {
    HkView
  },
  dicts: ['sys_normal_disable', 'platform_equipment_type', 'platform_location_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      LocationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否打开仓位预览
      veiwOpen: false,
      viewData: {
        loading: true,
        ip: "",
        port: "",
        userName: "",
        pwd: "",
        pan: "",
        tilt: "",
        iPresetID: "",
        locationId: ""
      },
      //弹框下拉选择框数据
      initData: {
        loading: true,
        row: null,
        equipmentListDefaultValue: null,
        equipmentList: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        storeLocationName: null,
        storeLocationCode: null,
        storeLocationStatus: null,
        useStatus: null,
        useInspection: null,
        useWarning: null,
        useWarningPush: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        equipmentId: [
          { required: true, message: "监控设备不能为空", trigger: "change" }
        ],
        storeLocationName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" },
          { min: 1, max: 25, message: "仓库名称名称长度不能超过25个汉字", trigger: "blur" }
        ],
        storeLocationCode: [
          { required: true, message: "仓位编码不能为空", trigger: "blur" },
          { min: 1, max: 36, message: "仓位编码长度不能超过36个数字或字母", trigger: "blur" }
        ],
        presetId: [
          { required: true, message: "预置点ID不能为空", trigger: "blur" }
        ],
        useStatus: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        useInspection: [
          { required: true, message: "是否开启自动巡检不能为空", trigger: "blur" }
        ],
        useWarning: [
          { required: true, message: "是否开启异常接收不能为空", trigger: "blur" }
        ],
        useWarningPush: [
          { required: true, message: "是否开启数字孪生异常推送不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否已删除(逻辑删除)不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listLocation(this.queryParams).then(response => {
        this.LocationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        storeLocationId: null,
        storeId: null,
        equipmentId: null,
        equipmentName: null,
        storeLocationName: null,
        storeLocationCode: null,
        presetId: null,
        pan: null,
        tilt: null,
        zoom: null,
        storeLocationStatus: null,
        useStatus: 1,
        useInspection: 1,
        useWarning: 1,
        useWarningPush: 1,
        quantity: null,
        memo: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeLocationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加仓位"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const storeLocationId = row.storeLocationId || this.ids
      getLocation(storeLocationId).then(response => {
        this.form = response.data
        this.initData.equipmentListDefaultValue = this.form.equipmentName
        this.open = true
        this.title = "修改仓位"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.storeLocationId != null) {
            updateLocation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addLocation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeLocationIds = row.storeLocationId || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + storeLocationIds + '"的数据项？').then(function () {
        return delLocation(storeLocationIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 删除按钮操作 */
    handleView(row) {
      const storeLocationId = row.storeLocationId || this.ids
      this.veiwOpen = true
      getLocation(storeLocationId).then(response => {
        this.viewData.loading = false
        this.viewData.ip = response.data.equipment.equipmentIp
        this.viewData.port = response.data.equipment.equipmentChannel
        this.viewData.userName = response.data.equipment.equipmentUserName
        this.viewData.pwd = response.data.equipment.equipmentPassword
        this.viewData.pan = response.data.pan
        this.viewData.tilt = response.data.tilt
        this.viewData.iPresetID = response.data.presetId
        this.viewData.locationId = response.data.storeLocationId
      })
    },
    viewClose() {
      this.viewData.open = false
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('platform/Location/export', {
        ...this.queryParams
      }, `Location_${new Date().getTime()}.xlsx`)
    },
    handleUse(row) {
      this.$modal.confirm('是否确认当前执行操作').then(function () {
        return updateLocation(row)
      }).then(() => {
        // this.getList()
        this.$modal.msgSuccess("操作成功")
      }).catch(() => { })
    },
    loadEquipmentList(key) {
      this.initData.loading = true
      const equipmentQueryParams = {
        key: key
      }
      listEquipment(equipmentQueryParams).then(response => {
        this.initData.equipmentList = response.rows
        this.initData.loading = false
      })
    }
  }
}
</script>
